#!/usr/bin/python
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup


class Parser(object):
    @staticmethod
    def render_url(url):
        """
        将url渲染成理想的url
        :param url:
        :return:
        """
        url = url if 'www.zhihu.com' in url else 'https://www.zhihu.com' + url  # 添加https协议到url
        url = url.split('/answer/')[0] if '/answer/' in url else url  # 转换答案为问题
        return url

    def parser(self, html, url):
        """
        网页文档解析器
        :param html: 网页文档
        :param url: 网页的url
        :return: (文档内的链接, 问题字典/None)
        """
        soup = BeautifulSoup(html, 'html.parser', from_encoding='utf-8')

        link_nodes = soup.select('a[href]')
        links = [
            self.render_url(link.get('href')) for link in link_nodes
            if 'question/' in link.get('href') or 'topic/' in link.get('href') or 'people/' in link.get('href')
            ]  # 筛选url
        if '/question/' in url:  # 获取问题
            try:
                question = dict()
                question['url'] = url
                question['question_id'] = str(url.split('/')[-1:][0])
                question['title'] = str(soup.find('h1').text)
                question['answer_num'] = str(soup.find(class_='List-headerText').text[:-4])
                question['followers_num'] = str(soup.find(class_='NumberBoard-value').text)
                question['top_answer'] = str(soup.find(class_='List-item'))
                question['top_answer_fn'] = str(soup.find(class_='VoteButton--up').text)
                question['topics'] = [
                    {
                        'topic_id': str(topic.attrs.get('href').split('/')[-1:][0]),
                        'name': str(topic.text),
                        'url': self.render_url(topic.attrs.get('href'))
                    } for topic in soup.find_all(class_='TopicLink')
                    ]
                return links, question
            except Exception, e:
                print e
                return links, None
        return links, None
